package com.wiener.web.managebeans;

import java.io.Serializable;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.UUID;  
  
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;  
  
import org.primefaces.event.RowEditEvent;  

import com.wiener.web.common.Car;
  

@ManagedBean(name = "tableBean")
@SessionScoped
public class TableBean implements Serializable {  
  
	private final static String[] institucion;
	private final static String[] mestrias;
	private final static String[] modalidad;
	
    private final static String[] colors;  
  
    private final static String[] manufacturers;  
  
    private List<Car> carsSmall;  
  
    static {  
    	institucion = new String[1];
    	institucion[0] = "Universidad Privada NOrbert";
    	
    	mestrias = new String[1];
    	mestrias[0] = "Administracion y Negocios Internacionles";
    	
    	modalidad = new String[1];
    	modalidad[0] = "Universidad Privada NOrbert";
    	
    	
        colors = new String[1];  
        colors[0] = "Universidad Privada Norbert";  
      
  
        manufacturers = new String[1];  
        manufacturers[0] = "Administracion y Negocios Internacionles";  
      
    }  
  
    public static String[] getMestrias() {
		return mestrias;
	}

	public static String[] getModalidad() {
		return modalidad;
	}

	public void setCarsSmall(List<Car> carsSmall) {
		this.carsSmall = carsSmall;
	}

	public TableBean() {  
        carsSmall = new ArrayList<Car>();  
  
        populateRandomCars(carsSmall, 1);  
    }  
  
    private void populateRandomCars(List<Car> list, int size) {  
        for(int i = 0 ; i < size ; i++)  
            list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));  
    }  
  
    public List<Car> getCarsSmall() {  
        return carsSmall;  
    }  
  
    private int getRandomYear() {  
        return (int) (Math.random() * 50 + 1960);  
    }  
  
    private String getRandomColor() {  
        return colors[0];  
    }  
  
    private String getRandomManufacturer() {  
        return manufacturers[0];  
    }  
  
    private String getRandomModel() {  
        return UUID.randomUUID().toString().substring(0, 8);  
    }  
  
    public String[] getManufacturers() {  
        return manufacturers;  
    }  
  
    public String[] getColors() {  
        return colors;  
    }  
  
    public static String[] getInstitucion() {
		return institucion;
	}

	public void onEdit(RowEditEvent event) {  
        FacesMessage msg = new FacesMessage("Car Edited", ((Car) event.getObject()).getModel());  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
      
    public void onCancel(RowEditEvent event) {  
        FacesMessage msg = new FacesMessage("Car Cancelled", ((Car) event.getObject()).getModel());  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
}  